package ch.sv7.tool.dbmanager.db.dialect;

import ch.sv7.tool.dbmanager.StringUtil;
import ch.sv7.tool.dbmanager.db.dialect.sybase.SybaseDialect;
import ch.sv7.tool.dbmanager.db.dialect.sybase.SybaseUnicodeDialect;
import ch.sv7.tool.dbmanager.types.TypesInfo;

/**
 * TODO: Document me!
 *
 * @author svt
 *
 */
public class DialectFactory {

	private static DialectFactory instance;
	
	private DialectFactory(){		
	}
	
	public static DialectFactory getInstance(){
		if (instance == null){
			instance = new DialectFactory();
		}
		return instance;
	}
	
	public Dialect getDialect(String name, TypesInfo typesInfo, String properties){
		Dialect dialect = null; 
		
		if ("sybase".equals(name)){
			dialect = new SybaseDialect(typesInfo);
		} else if ("sybase-unicode".equals(name)){
			dialect = new SybaseUnicodeDialect(typesInfo);
		} else {
			dialect = new DefaultDialect(typesInfo);
		}
		
		if (properties != null && properties.trim().length() > 0){
			String[] props = StringUtil.commaSeparatedToArray(properties);
			for (int i=0; i< props.length; i++) {
				int sep = props[i].indexOf("=");
				if (sep > 0){
					String key = props[i].substring(0, sep).trim();
					String value = props[i].substring(sep + 1).trim();
					dialect.setProperty(key, value);
				}
			}
		}
		
		return dialect;
	}
	
	public Dialect getDialect(String name, TypesInfo typesInfo){
		return getDialect(name, typesInfo, null);
	}
	
}
